package cn.maidouya.user.controller;

import cn.maidouya.basic.exception.BusinessExcepiton;
import cn.maidouya.basic.util.AjaxResult;
import cn.maidouya.user.dto.LoginDto;
import cn.maidouya.user.dto.UserDto;
import cn.maidouya.user.service.ILoginService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private ILoginService loginService;

    /**
     *    手机号码登录信息
     * @param loginDto
     * @return
     */
    @PostMapping("/account")
    public AjaxResult accountLogin (@RequestBody LoginDto loginDto){
        try {
            Map<String,Object> map =loginService.accountLogin(loginDto);
            return AjaxResult.getResult().setResultObj(map);
        } catch (BusinessExcepiton e) {
            e.printStackTrace();
            return AjaxResult.getResult().setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getResult().setMessage("系统繁忙，请稍后重试！！");
        }
    }

    /**
     *   微信登录
     * @param param
     * @return
     */
    @PostMapping("/wechat")
    public AjaxResult wechatLogin(@RequestBody Map<String, String> param){
        try {
            return loginService.wechatLogin(param.get("code"));
        } catch (BusinessExcepiton e) {
            e.printStackTrace();
            return AjaxResult.getResult().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getResult().setMessage("系统繁忙，请稍后再试！！");
        }
    }

    /**
     *   绑定微信用户
     * @param param
     * @return
     */
    @PostMapping("/binder/wechat")
    public AjaxResult binderWechat (@RequestBody Map<String, String> param){
        try {
            Map<String, Object> map = loginService.binderWechat(param);
            return AjaxResult.getResult().setResultObj(map);
        } catch (BusinessExcepiton e) {
            e.printStackTrace();
            // service业务异常
            return AjaxResult.getResult().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getResult().setMessage("系统繁忙，请稍后再试！！");
        }
    }



}